#include "window-room.h"
#include "shape.h"
#include <librsvg/rsvg.h>

void
window_room_box_chat_init(struct window_room_s *window)
{
	window->entry_chat = gtk_entry_new();
	window->button_send = gtk_button_new_with_label("Send");

	window->scrolled_chat = gtk_scrolled_window_new(FALSE, FALSE);
	window->text_chat = gtk_text_view_new();
	gtk_widget_set_size_request(window->text_chat, -1, 80);
	gtk_container_add(window->scrolled_chat, window->text_chat);

	window->box_text = gtk_hbox_new(FALSE, 2);
	gtk_box_pack_start(window->box_text, window->entry_chat, TRUE, TRUE, 2);
	gtk_box_pack_start(window->box_text, window->button_send, FALSE, FALSE, 2);

	window->box_chat = gtk_vbox_new(FALSE, 2);
	gtk_box_pack_start(window->box_chat, window->scrolled_chat, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_chat, window->box_text, FALSE, FALSE, 2);

}

gboolean
window_room_view_toolbox_focused(GtkWidget *widget,
								 GtkDirectionType arg1,
								 gpointer data)
{
	struct window_room_s *window = (struct window_room_s *)data;
	gtk_toggle_button_set_active(window->button_tool_select, FALSE);

	return TRUE;
}

void
window_room_toolbox_init(struct window_room_s *window)
{
	GList *shapes = shape_library;
	shape_t *shape;
	RsvgHandle *handle;
	GdkPixbuf *pixbuf, *pixbuf_scaled;
	GtkTreeIter iter;
	GtkCellRenderer *renderer;

	window->list_toolbox = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);

	while (shapes) {
		shape = (shape_t *)shapes->data;
		handle = rsvg_handle_new_from_file(shape->filename, NULL);
		pixbuf = rsvg_handle_get_pixbuf(handle);
		pixbuf_scaled = gdk_pixbuf_scale_simple(pixbuf, 32, 32, GDK_INTERP_BILINEAR);
		gtk_list_store_append(window->list_toolbox, &iter);
		printf("ID: %s\n", shape->ID);
		gtk_list_store_set(window->list_toolbox, &iter, 0, pixbuf_scaled, 1, shape->ID, -1);
		shapes = shapes->next;
	}

	window->view_toolbox = gtk_tree_view_new();
	gtk_widget_add_events(window->view_toolbox, GDK_ALL_EVENTS_MASK);

	renderer = gtk_cell_renderer_pixbuf_new();
	gtk_tree_view_insert_column_with_attributes(window->view_toolbox, 
											    -1, "Preview", renderer,
												"pixbuf", 0,
												NULL);
	renderer = gtk_cell_renderer_text_new();
	gtk_tree_view_insert_column_with_attributes(window->view_toolbox, 
											    -1, "Name", renderer,
												"text", 1,
												NULL);
	gtk_tree_view_set_model(window->view_toolbox, GTK_TREE_MODEL(window->list_toolbox));
	g_signal_connect(window->view_toolbox, "row-activated", window_room_view_toolbox_selected, window);
	window->scrolled_toolbox = gtk_scrolled_window_new(FALSE, FALSE);
	g_signal_connect(window->scrolled_toolbox, "focus", window_room_view_toolbox_focused, window);
	gtk_container_add(window->scrolled_toolbox, window->view_toolbox);
}

void
window_room_view_toolbox_selected(GtkTreeView *view,
								  GtkTreePath *path,
								  GtkTreeViewColumn *column,
								  gpointer data)
{
	struct window_room_s *window = (struct window_room_s *)data;
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	char *name;

	selection = gtk_tree_view_get_selection(window->view_toolbox);
	gtk_tree_selection_get_selected(selection, &window->list_toolbox, &iter);
	gtk_tree_model_get(window->list_toolbox, &iter, 1, &name, -1);
	printf("Selected: %s\n", name);
	gtk_whiteboard_shape_select(window->wb, name);
	g_free(name);
}
void
window_room_button_tool_del_clicked(GtkButton *button,
									   gpointer data)
{
	struct window_room_s *window = (struct window_room_s *)data;
	printf("Deleting\n");
	gtk_whiteboard_shape_delete(window->wb);
}

void
window_room_button_tool_arrow_clicked(GtkButton *button,
									   gpointer data)
{
	struct window_room_s *window = (struct window_room_s *)data;
	printf("Using arrow tool");
	window->wb->arrow_tool = TRUE;
}


void
window_room_button_tool_select_toggled(GtkToggleButton *button,
									   gpointer data)
{
	struct window_room_s *window = (struct window_room_s *)data;
	if (gtk_toggle_button_get_active(button))
		window->wb->select_tool = TRUE;
	else
		window->wb->select_tool = FALSE;
		
}

void
window_room_property_tab_init(struct window_room_s *window)
{
	window->box_property = gtk_vbox_new(FALSE, 2);

}

void
window_room_init()
{
	struct window_room_s *window;

	window = g_malloc(sizeof(struct window_room_s));

	window->wb = gtk_whiteboard_new();

	window->button_leave = gtk_button_new_with_label("Leave");
	window->label_user = gtk_label_new("Logged in as ...");

	window->box_top = gtk_hbox_new(FALSE, 2);
	gtk_box_pack_start(window->box_top, window->label_user, FALSE, FALSE, 2);
	gtk_box_pack_end(window->box_top, window->button_leave, FALSE, FALSE, 2);

	shape_load_dir("../data");

	window_room_toolbox_init(window);

	window->button_tool_select = gtk_toggle_button_new();
	g_signal_connect(window->button_tool_select, "toggled", window_room_button_tool_select_toggled, window);
	gtk_toggle_button_set_mode(window->button_tool_select, FALSE);
	gtk_button_set_image(window->button_tool_select, gtk_image_new_from_file("../data/pointer.png"));

	window->button_tool_del = gtk_button_new();
	g_signal_connect(window->button_tool_del, "clicked", window_room_button_tool_del_clicked, window);
	gtk_button_set_image(window->button_tool_del, gtk_image_new_from_file("../data/delete.png"));
	
	window->button_tool_arrow = gtk_button_new();
	g_signal_connect(window->button_tool_arrow, "clicked", window_room_button_tool_arrow_clicked, window);
	gtk_button_set_image(window->button_tool_arrow, gtk_image_new_from_file("../data/arrow.png"));

	window->box_tools = gtk_hbox_new(FALSE, 1);
	gtk_box_pack_start(window->box_tools, window->button_tool_select, FALSE, FALSE, 1);
	gtk_box_pack_start(window->box_tools, window->button_tool_del, FALSE, FALSE, 1);
	gtk_box_pack_start(window->box_tools, window->button_tool_arrow, FALSE, FALSE, 1);

	window->tab_side = gtk_notebook_new();
	gtk_notebook_append_page(window->tab_side, window->scrolled_toolbox, gtk_label_new("Shapes"));

	window->box_sidebar = gtk_vbox_new(FALSE, 2);
	gtk_widget_set_size_request(window->box_sidebar, 200, -1);
	gtk_box_pack_start(window->box_sidebar, window->box_tools, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_sidebar, window->tab_side, TRUE, TRUE, 2);
	window->box_contents = gtk_hbox_new(FALSE, 2);
	gtk_box_pack_start(window->box_contents, window->box_sidebar, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_contents, window->wb->scrolled, TRUE, TRUE, 2);

	window_room_box_chat_init(window);
	window->box_main = gtk_vbox_new(FALSE, 2);
	gtk_box_pack_start(window->box_main, window->box_top, FALSE, FALSE, 2);
	gtk_box_pack_start(window->box_main, window->box_contents, TRUE, TRUE, 2);
	gtk_box_pack_start(window->box_main, window->box_chat, FALSE, FALSE, 2);

	window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_container_add(window->window, window->box_main);
	gtk_window_set_default_size(window->window, 700, 600);
	gtk_container_set_border_width(window->window, 2);
	gtk_window_set_title(window->window, "In room ...");
	gtk_widget_show_all(window->window);
}

